#!/usr/bin/env python

from __future__ import print_function, division, unicode_literals

__doc__ = """filter geneinfo files by given criteria"""

import argparse
import os
import sys

import uta.formats.geneinfo as ufgi


def parse_args(argv):
    ap = argparse.ArgumentParser(
        description=__doc__,
    )
    ap.add_argument(
        "FILE"
    )
    ap.add_argument(
        "--genes-from", "-G",
    )

    opts = ap.parse_args(argv)
    return opts


if __name__ == "__main__":
    opts = parse_args(sys.argv[1:])

    filter_genes = set()
    if opts.genes_from:
        filter_genes = set([l.strip()
                            for l in open(opts.genes_from, "r").readlines()])

    gir = ufgi.GeneInfoReader(
        sys.stdin if opts.FILE == "-" else open(opts.FILE, "r"))
    giw = ufgi.GeneInfoWriter(sys.stdout)

    for gi in gir:
        if gi.hgnc in filter_genes:
            giw.write(gi)
